{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>

    <form class="layui-form layui-form-pane" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">会议名称</label>
            <div class="layui-input-4">
                <input type="text" name="name" value="{$info['name']}" lay-verify="required" placeholder="{:lang('pleaseEnter')}会议名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">会议地址</label>
            <div class="layui-input-4">
                <input type="text" name="address" value="{$info['address']}" lay-verify="required" placeholder="{:lang('pleaseEnter')}会议地址" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">参加人员</label>
            <div class="layui-input-4">
                <div class="layui-input-inline" style="width: 100%">
                    <input type="radio" name="person"  lay-filter="all" {if $info.person=="0" }checked{/if} value="0" title="所有人" checked>
                    <input type="radio" name="person"  lay-filter="appoint" {if $info.person!="0" }checked{/if} value="1" title="特定对象">
                    <input type="button" value="添加"  {if $info.person=="0" }hidden{/if} id="object" class="layui-btn layui-btn-primary layui-btn-sm">
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space3 ob hidden"style="margin-bottom: 10px">
            <div class="layui-col-md1">
            </div>
            <div class="layui-col-md5 per" style="margin-left: -22px;">

            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">打卡设备</label>
            <div class="layui-input-inline">
                <select name="device_id" lay-verify="required" lay-filter="device_id" >
                    {volist name="deviceGroup" id="vo"}
                    <option value="{$vo.id}" {if $info.devicce_id==$vo.id}selected{i/f}>{$vo.device_name}</option>
                    {/volist}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">打卡时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" value="{$info['meeting_time']}" id="sign_time" placeholder="yyyy-MM-dd">
            </div>
            <div class="layui-input-inline">
                <input type="text"  name="sign_start_time" value="{$info['sign_start_time']}"  class="layui-input" id="sign_start_time" placeholder="H：m">
            </div>

            <div class="layui-input-inline" style="margin-top: 9px;">
                <span>至</span>
            </div>
            <div class="layui-input-inline" style="margin-top: -1px;margin-left:-174px;">
                <input type="text"  name="sign_end_time"  value="{$info['sign_end_time']}" class="layui-input" id="sign_end_time" placeholder="H：m">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">会议时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" value="{$info['meeting_time']}" id="meeting_time" placeholder="yyyy-MM-dd">
            </div>
            <div class="layui-input-inline">
                <input type="text"  name="meeting_start_time" value="{$info['meeting_start_time']}"  class="layui-input" id="meeting_start_time" placeholder="H:m">
            </div>

            <div class="layui-input-inline" style="margin-top: 9px;">
                <span>至</span>
            </div>
            <div class="layui-input-inline" style="margin-top: -1px;margin-left:-174px;">
                <input type="text"  name="meeting_end_time"  value="{$info['meeting_end_time']}" class="layui-input" id="meeting_end_time" placeholder="H:m">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="id" id="id" value="{$id}">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a href="{:url('lists')}" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>

</div>
{include file="common/foot"/}

<script>

    // 父窗口变量 用于接收子窗口传递的数据
    var rel="";
    // 存放特定规则对象
    var person = new Array();

    $(function () {

        // 发起aiax请求数组数据
        var id = $("#id").val();
        console.log(id);
        $.post('{:url("meetingEdit")}',{id:id},function (res) {
            //初始化特定对象
            if(res!=''){
                var str = "" ;
                for (var i=0;i<res.length;i++){

                    if($.inArray(res[i]['id'], person)==-1){
                        person.push(res[i]['id']);
                        str = str+"                <span class=\"layui-badge-rim\" id=sp"+res[i]['id']+" style=\"margin-left: 10px;width: 100px;padding-top: 8px;padding-bottom: 8px\">"+res[i]['true_name']+"<i class=\"layui-icon\" onclick='del("+res[i]['id']+")'  style='float: right;'>&#x1006;</i> </span>\n";
                    }
                }
                $(".ob").show();
                $(".per").css('border','solid 1px #f2f2f2');
                $(".per").css('border-radius','15px');
                $(".per").append(str);
            }else{
                $(".per").css('border','none');
                $(".per").css('border-radius','none');
            }


        });

    });

    //用于子窗体向父窗体赋值
    function setRel(rel){

        this.rel=rel;

    }

    //删除某一特定规则对象
    function del(i) {

        this.person.splice($.inArray(i,this.person),1);
        $("#sp"+i).remove();

    }

    layui.use(['form','laydate', 'layer','upload'], function () {
        var form = layui.form, layer = layui.layer,$= layui.jquery,upload = layui.upload,laydate = layui.laydate;

        var info = {$info|raw};
        form.val("form", info);
        form.render();

        laydate.render({
            elem: '#sign_time'
            ,format: 'yyyy-MM-dd'
        });
        laydate.render({
            elem: '#meeting_time'
            ,format: 'yyyy-MM-dd'
        });
        laydate.render({
            elem: '#sign_start_time'
            ,type: 'time'
            ,format: 'H:m'
        });
        laydate.render({
            elem: '#sign_end_time'
            ,type: 'time'
            ,format: 'H:m'
            ,done: function(value, date){
                if($("#sign_start_time").val()>value){
                    layer.alert('请输入正确的打卡时间！');
                }

            }
        });
        laydate.render({
            elem: '#meeting_start_time'
            ,type: 'time'
            ,format: 'H:m'
        });
        laydate.render({
            elem: '#meeting_end_time'
            ,type: 'time'
            ,format: 'H:m'
            ,done: function(value, date){
                if($("#meeting_start_time").val()>value){
                    layer.alert('请输入正确的会议时间！');
                }

            }
        });
        // 监听特定radio
        form.on('radio(appoint)', function(data){
            if (data.value){
                $('#object').show();
            }
        });

        // 监听全部radio
        form.on('radio(all)', function(data){
            if (data.value){
                $('#object').hide();
                $('.ob').hide();
                this.person='';
            }
        });

        // 监听添加特定对象按钮
        $('#object').on('click', function () {

            layer.open({
                title: '选择特定对象'
                ,type: 2
                ,area: ['800px', '800px']
                ,shadeClose : true
                ,content: '{:url("teacher")}'
            });

        });

        // 监听提交按钮
        form.on('submit(submit)', function (data) {
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var sign_start_time    = $('#sign_start_time').val();
            var sign_end_time      = $('#sign_end_time').val();
            var meeting_start_time = $('#meeting_start_time').val();
            var meeting_end_time   = $('#meeting_end_time').val();

            if((sign_start_time!='')&&sign_end_time!=''&&meeting_start_time!=''&&meeting_end_time!=''&&(sign_start_time<sign_end_time)&&(meeting_start_time<meeting_end_time)){
                if(person==''){
                    var d2 =  0;
                }else{
                    for (var i=0;i<person.length;i++){
                        if(person[i]==''){
                            person.splice(i,1);
                        }
                    }
                    var d2 =  JSON.stringify(person);
                }


                data.field.person = d2;
                data.field.sign_start_time      = $("#sign_time").val()+" "+data.field.sign_start_time;
                data.field.sign_end_time        = $("#sign_time").val()+" "+data.field.sign_end_time;
                data.field.meeting_start_time   = $("#meeting_time").val()+" "+data.field.meeting_start_time;
                data.field.meeting_end_time     = $("#meeting_time").val()+" "+data.field.meeting_end_time;

                $.post("{:url(meetingSave)}",data.field, function (res) {
                    layer.close(loading);
                    if (res.code > 0) {
                        layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                            location.href = res.url;
                        });
                    } else {
                        layer.msg(res.msg, {time: 1800, icon: 2});
                    }
                });
            }else{
                layer.alert('时间有误');
                layer.close(loading);
                return false;
            }

        });


        // 监听子窗口对父窗口的数据传递
        setInterval(function () {

            if( this.rel!=''){
                var temp = JSON.parse(this.rel);
                var str = "" ;

                for (var i=0;i<temp.length;i++){

                    if($.inArray(temp[i]['id'], person)==-1){
                        person.push(temp[i]['id']);
                        str = str+"                <span class=\"layui-badge-rim\" id=sp"+temp[i]['id']+" style=\"margin-left: 10px;width: 100px;padding-top: 8px;padding-bottom: 8px\">"+temp[i]['true_name']+"<i class=\"layui-icon\" onclick='del("+temp[i]['id']+")'  style='float: right;'>&#x1006;</i> </span>\n";
                    }
                }
                $(".ob").show();
                $(".per").css('border','solid 1px #f2f2f2');
                $(".per").css('border-radius','15px');
                $(".per").append(str);
                // 表单重新渲染，要不然添加完显示不出来新的option
                form.render();
            }
            this.rel='';
        },3000);

    });


</script>